﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace RpgEditor
{
    public class war
    {
        public ArrayList data = new ArrayList();
        mainForm form = null;
        private String fn;
        public war(mainForm f)
        {
            form = f;
        }
        public void setFn(String fn)
        {
            this.fn = fn;
            form.textBox24.Text = fn;
        }

        public String getFn()
        {
            return fn;
        }

        public void init()
        {
            data = new ArrayList();
        }

        public void save()
        {
            FileStream fs1 = new FileStream(fn, FileMode.Create, FileAccess.Write);
            BinaryWriter dis = new BinaryWriter(fs1, System.Text.Encoding.Default);

            MyIO.writeByte(dis,(SByte)data.Count);

            for (int i = 0; i < data.Count; i++)
            {
                SByte[] t = (SByte[])data[i];
                

                for (int j = 0; j < 8; j++)
                {
                    MyIO.writeByte(dis, t[j]);
                }
            }

            dis.Close();

        }

        public void getwarInfo(String fn)
        {

            FileStream fs1 = new FileStream(fn, FileMode.Open, FileAccess.Read);
            BinaryReader dis = new BinaryReader(fs1, System.Text.Encoding.Default);
            
            int num = MyIO.readByte(dis);
			
            data = new ArrayList();
			
			for(int i=0;i<num;i++){
                SByte[] t = new SByte[8];
                data.Add(t);
				
                for (int j = 0; j < 8; j++)
                {
                    t[j] = MyIO.readByte(dis);
                }
			}

            dis.Close();

        }

        public void display()
        {
            
            form.listBox10.Items.Clear();
            for (int i = 0; i < data.Count; i++)
            {
                SByte[] t = (SByte[])(data[i]);
                form.listBox10.Items.Add(t[0]);
            }
            if (data.Count > 0)
            {
                form.listBox10.SelectedIndex = 0;

                displayatt(0);
            }
        }

        public void displayatt(int i)
        {
            if (i >= data.Count)
            {
                return;
            }
            SByte[] t = (SByte[])data[i];
            form.textBox30.Text = "" + t[0];
            form.textBox31.Text = "" + t[1];
            form.textBox32.Text = "" + t[2];
            form.textBox33.Text = "" + t[3];
            form.comboBox5.SelectedIndex = t[4];
            form.textBox35.Text = "" + t[5];
            form.textBox36.Text = "" + t[6];
            form.textBox37.Text = "" + t[7];
        }

        public void up(int i)
        {
            if (i >= data.Count)
            {
                return;
            }
            SByte[] t = (SByte[])data[i];
            t[0] = SByte.Parse(form.textBox30.Text);
            t[1] = SByte.Parse(form.textBox31.Text);
            t[2] = SByte.Parse(form.textBox32.Text);
            t[3] = SByte.Parse(form.textBox33.Text);
            t[4] = (SByte)(form.comboBox5.SelectedIndex);
            t[5] = SByte.Parse(form.textBox35.Text);
            t[6] = SByte.Parse(form.textBox36.Text);
            t[7] = SByte.Parse(form.textBox37.Text);
        }

        public void add()
        {

            SByte[] t = new SByte[8];
            t[0] = SByte.Parse(form.textBox30.Text);
            t[1] = SByte.Parse(form.textBox31.Text);
            t[2] = SByte.Parse(form.textBox32.Text);
            t[3] = SByte.Parse(form.textBox33.Text);
            t[4] = (SByte)(form.comboBox5.SelectedIndex);
            t[5] = SByte.Parse(form.textBox35.Text);
            t[6] = SByte.Parse(form.textBox36.Text);
            t[7] = SByte.Parse(form.textBox37.Text);
            data.Add(t);
        }
    }
}
